/*
 * Created by SharpDevelop.
 * user: Robertux
 * Date: 12/04/2008
 * Time: 21:52
 * 
 * 
 */

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using Inventador;
using DataLayer;

namespace WinApp
{
	/// <summary>
	/// Description of NewsView.
	/// </summary>
	public class NewsView: Panel
	{
		#region Campos

        public bool relax = false; //TRUE para que al iniciar, el control no haga nada.

		private PictureBox pbxLogo;
		private Label lblTituloUltimosMovs;
		private LinkLabel lblMasMovs;
		private Label lblTituloProdsReq;
		private LinkLabel lblMasProds;
		private List<Movimiento> ultimosMovs;
		private List<Producto> prodsBajoExist;
		private BaseDatos bdatos;
		
		public delegate void JustRaise();
		
		public event JustRaise evMasMovsClick;
		public event JustRaise evMasProdsClick;
		
		#endregion
		
		#region Propiedades
		#endregion
		
		#region Metodos
		
		public NewsView()
		{
			this.pbxLogo = new PictureBox();
			this.lblTituloUltimosMovs = new Label();
			this.lblTituloProdsReq = new Label();
			this.lblMasMovs = new LinkLabel();
			this.lblMasProds = new LinkLabel();
			this.bdatos = new BaseDatos();
            //<relax>
            if (relax != true) //HACK: HACK!
                this.EsteProcesoEsMuyLento();
            this.relax = false;
            //</relax>
			//<formato>
			this.pbxLogo.ImageLocation = Application.StartupPath + "\\recursos\\FondoInventador.png";
			this.pbxLogo.Height = 376;
			this.pbxLogo.Width = 642;
			this.Height = 376;
			this.Width = 642;			
			this.lblTituloUltimosMovs.Text = "Ultimos Movimientos \nde Este Mes";
			this.lblTituloUltimosMovs.AutoSize = true;
			this.lblTituloUltimosMovs.BackColor = Color.White;
			this.lblTituloUltimosMovs.ForeColor = Color.DarkBlue;
			this.lblTituloUltimosMovs.Font = new Font(FontFamily.GenericSansSerif, 12, FontStyle.Bold);
			this.lblTituloUltimosMovs.Left = 35;
			this.lblTituloUltimosMovs.Top = 160;
			
			this.lblTituloProdsReq.Text = "Productos Abajo de \nsu Existencia Minima";
			this.lblTituloProdsReq.BackColor = Color.White;
			this.lblTituloProdsReq.ForeColor = Color.DarkBlue;
			this.lblTituloProdsReq.Font = new Font(FontFamily.GenericSansSerif, 12, FontStyle.Bold);
			this.lblTituloProdsReq.Left = 350;
			this.lblTituloProdsReq.Top = 160;
			this.lblTituloProdsReq.AutoSize = true;
			
			this.lblMasMovs.Text = "Ver todos los Movimientos...";
			this.lblMasMovs.AutoSize = true;
			this.lblMasMovs.BackColor = Color.White;
			this.lblMasMovs.Left = 35;
			this.lblMasMovs.Top = 350;
			
			this.lblMasProds.Text = "Ver Todos los Productos...";
			this.lblMasProds.AutoSize = true;
			this.lblMasProds.BackColor = Color.White;
			this.lblMasProds.Left = 350;
			this.lblMasProds.Top = 350;
			
			//</formato>
			this.AddMovs();
			this.AddProds();
			this.Controls.Add(this.lblTituloUltimosMovs);
			this.Controls.Add(this.lblTituloProdsReq);
			this.Controls.Add(this.lblMasMovs);
			this.Controls.Add(this.lblMasProds);
			this.Controls.Add(this.pbxLogo);
						
			this.lblMasMovs.Click += this.OnLblMasMovsClick;
			this.lblMasProds.Click += this.OnLblMasProdsClick;
			this.evMasMovsClick += OnMasMovsClick;
			this.evMasProdsClick += OnMasProdsClick;
			
			this.HideNews();
		}

        public void EsteProcesoEsMuyLento() //TODO: para que te de curiosidad y lo veas! xD
        {
            this.ultimosMovs = Movimiento.GetList(this.bdatos, "id!=0", "fecha DESC");
            for (int i = 0; i < this.ultimosMovs.Count; i++)
            {
                if (this.ultimosMovs[i].Fecha.Year != DateTime.Now.Year)
                {
                    this.ultimosMovs.RemoveAt(i);
                    i = 0;
                }
                else
                {
                    if (this.ultimosMovs[i].Fecha.Month != DateTime.Now.Month)
                    {
                        this.ultimosMovs.RemoveAt(i);
                        i = 0;
                    }
                }
            }
            this.prodsBajoExist = Producto.GetList(this.bdatos, "id!=0 AND existencia_actual < existencia_minima");
        }
		
		public void ShowNews()
		{
            if (relax == false)
                this.EsteProcesoEsMuyLento();
			this.lblTituloUltimosMovs.Visible = true;
			this.lblTituloProdsReq.Visible = true;
			this.lblMasMovs.Visible = true;
			this.lblMasProds.Visible = true;
			foreach(Control ctrl in this.Controls)
			{
				if(ctrl.Name.Contains("lblMov") || ctrl.Name.Contains("lblProd"))
					ctrl.Visible = true;
			}
			this.lblTituloUltimosMovs.Text = "Ultimos Movimientos \nde Este Mes(" + this.ultimosMovs.Count.ToString() + ")";
			this.lblTituloProdsReq.Text = "Productos Abajo de \nsu Existencia Minima(" + this.prodsBajoExist.Count.ToString() + ")";
		}
		
		public void HideNews()
		{
			this.lblTituloUltimosMovs.Visible = false;
			this.lblTituloProdsReq.Visible = false;
			this.lblMasMovs.Visible = false;
			this.lblMasProds.Visible = false;
			foreach(Control ctrl in this.Controls)
			{
				if(ctrl.Name.Contains("lblMov") || ctrl.Name.Contains("lblProd"))
					ctrl.Visible = false;
			}
			this.lblTituloUltimosMovs.Text = "Ultimos Movimientos \nde Este Mes";
			this.lblTituloProdsReq.Text = "Productos Abajo de \nsu Existencias Minima";
		}
		
		public void AddMovs()
		{
            try
            {
                for(int i = 0; i<this.ultimosMovs.Count; i++)
			{
				if(i<7)
				{
					Label lblMov = new Label();
					lblMov.Text = this.ultimosMovs[i].Fecha.ToShortDateString() + " \t" + this.ultimosMovs[i].fkProducto.fkTipoProducto.Nombre + " " + this.ultimosMovs[i].fkProducto.fkMarcaProducto.Nombre + "(";					
					if(this.ultimosMovs[i].TipoMovimiento == "Entrada")
					{
						lblMov.Text += "+";
						lblMov.ForeColor = Color.Green;
					}
					else
					{
						lblMov.Text += "-";
						lblMov.ForeColor = Color.DarkRed;
					}
					lblMov.Text += this.ultimosMovs[i].Cantidad.ToString() + ")";
					lblMov.BackColor = Color.White;
					lblMov.AutoSize = true;
					lblMov.Name = "lblMov" + i.ToString();
					lblMov.Left = 35;
					lblMov.Top = 190 + ((i + 1) * 20);
					this.Controls.Add(lblMov);
				}
				else
					break;
			    }
            }
            catch (Exception)
            {
                //...
            }
		}
		
		public void AddProds()
		{
            try
            {
                for (int i = 0; i < this.prodsBajoExist.Count; i++)
                {
                    if (i < 7)
                    {
                        Label lblProd = new Label();
                        lblProd.Text = this.prodsBajoExist[i].fkTipoProducto.Nombre + " " + this.prodsBajoExist[i].fkMarcaProducto.Nombre;
                        lblProd.Text += " \t Min: " + this.prodsBajoExist[i].ExistenciaMinima.ToString();
                        lblProd.Text += " Actual: " + this.prodsBajoExist[i].ExistenciaActual.ToString();
                        lblProd.BackColor = Color.White;
                        lblProd.AutoSize = true;
                        lblProd.Name = "lblProd" + i;
                        lblProd.Left = 350;
                        lblProd.Top = 190 + ((i + 1) * 20);
                        this.Controls.Add(lblProd);
                    }
                    else
                        break;
                }
            }
            catch (Exception)
            {
                //...
            }
			
		}
		
		public void OnLblMasMovsClick(object sender, EventArgs e)
		{
			if(this.evMasMovsClick != null)
				this.evMasMovsClick();
		}
		
		public void OnLblMasProdsClick(object sender, EventArgs e)
		{
			if(this.evMasProdsClick != null)
				this.evMasProdsClick();
		}
		
		public void OnMasMovsClick(){ }
		
		public void OnMasProdsClick(){ }
		
		#endregion
	}
}
